package com.cpi.cornpp.plants.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.plants.form.PlantQueryForm;
import com.cpi.cornpp.plants.services.PlantService;

/**
 * Plant List Action
 * @author Edgar Joao
 *
 */
public class PlantListAction extends Action{

	/** Default forward*/
	private final static String DEFAULT = "default";
	
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PlantService service = null;
		List listPlantViews = null;
		PlantQueryForm plantQueryForm = null;
		HttpSession httpSession = null;
		
		listPlantViews = new ArrayList(0);
		service = new PlantService();
		
		if(form != null){
			plantQueryForm = (PlantQueryForm) form;
			httpSession = request.getSession(false);
			listPlantViews = service.list(plantQueryForm.getOrderField(),(1 == plantQueryForm.getOrderType()) ? false : true);
			request.setAttribute("listPlantViews", listPlantViews);
		}
		httpSession.setAttribute("previousOrderField", Integer.toString(plantQueryForm.getOrderField()));
		httpSession.setAttribute("previousOrderType", Integer.toString(plantQueryForm.getOrderType()));
		return mapping.findForward(DEFAULT);
	}
}
